package lc.q51_100;

import java.util.Arrays;

public class Q66 {

    public int[] plusOne(int[] digits) {
        int k = 1;
        final int length = digits.length;
        for (int i = length; i > 0; i--) {
            digits[i - 1] += k;
            k = digits[i-1] / 10;
            if (k == 0) {
                return digits;
            }
            digits[i - 1] = digits[i - 1] % 10;
        }
        if (k > 0) {
            int[] newDigits = new int[digits.length+1];
            newDigits[0] = k;
            System.arraycopy(digits, 0, newDigits, 1, digits.length);
            return newDigits;
        } else {
            return digits;
        }
    }

}
